﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Data.Entity;

namespace MysqlTest
{
    [Serializable]
    public class OrderService 
    {

        public void DeleteOrder(int ID)
        {
            using(var context=new OrdersContext())
            {
                var order = context.orders.Include("items").FirstOrDefault(p => p.OrderID == ID);
                if (order != null)
                {
                    context.orders.Remove(order);
                    context.SaveChanges();
                }
            }
        }


        public void AddOrder(object e)
        {
            Order m = e as Order;
            if (m == null)
            {
                Console.WriteLine("这不是一个订单！！！");
                throw new Exception("输入错误！！！");
            }
            else
            {
                using(var context=new OrdersContext())
                {
                    context.orders.Add(m);
                    context.SaveChanges();
                }
            }

        }


        public List<Order> SearchOrder(int ID)
        {
            using (var context = new OrdersContext())
            {
                var query = context.orders.Include("items").SingleOrDefault(p => p.OrderID == ID);
                List<Order> result = new List<Order> { query };
                return result;
            }
        }


        public List<Order> SearchOrder(string kind, string name)
        {
            if (kind == "CustomerName")
            {
                using (var context = new OrdersContext())
                {
                    var query = context.orders.Include("items")
                                .Where(p => p.CustomerName == name)
                                .OrderBy(p => p.Sum);
                    List<Order> result = query.ToList();
                    return result;

                }
            }
            else if (kind == "ProductName")
            {
                using (var context = new OrdersContext())
                {
                    var query = from order in context.orders
                                where (from orderdeteil in order.OrderDetailsList
                                       where orderdeteil.ProductName == name
                                       select order.OrderID).Contains(order.OrderID)
                                orderby order.Sum
                                select order;
                    List<Order> result = query.ToList();
                    return result;
                }
            }
            else
            {
                throw new Exception("输入kind错误");
                //return null;
            }
        }


        public void ChangeOrder(object e)
        {
            Order m = e as Order;
            using (var context=new OrdersContext())
            {
                var update = context.orders.FirstOrDefault(p => p.Equals(m));
                if (update != null)
                {
                    update = m;
                    context.SaveChanges();
                }
            }
        }


    }
}
